import * as Cesium from "cesium";
import CesiumEntityCollection from "../common/CesiumEntityCollection";
import TipDOM from "./TipDOM";
import CesiumScreenHandeler from "../common/CesiumScreenHandeler";
import CesiumCaculater from "../common/CesiumCaculater";
import CesiumCrood from "../common/CesiumCrood";
import DrawPoint from "./DrawChildren/DrawPoint";
import DrawPolyline from "./DrawChildren/DrawPolyline";
import DrawPolygon from "./DrawChildren/DrawPolygon";
import DrawRectangle from "./DrawChildren/DrawRectangle";
import DrawCircle from "./DrawChildren/DrawCircle";
import DrawPointHeight from "./DrawChildren/DrawPointHeight";

/**
 * 封装Cesium绘图测量工具
 * 目前有绘制、测量
 * 1.点
 * 2.折线
 * 3.多边形
 * 4.矩形
 * 5.圆形
 * 6.两点间高度测量
 */
export default class CesiumDraw {
  constructor(viewer) {
    // 组合引入其他封装
    this._handler = new CesiumScreenHandeler(viewer); // 二次封装cesium 屏幕键盘事件处理
    this._caculater = new CesiumCaculater(viewer); // 地理空间计算
    this._coorder = new CesiumCrood(viewer); // 坐标转化方法
    this._tip = new TipDOM(viewer); // 文字提示dom
    this.viewer = viewer;
    this.ellipsoid = Cesium.Ellipsoid.WGS84;

    // 自定义了事件常量
    this.CustomType = {
      POINT: "Point",
      POLYLINE: "Polyline",
      POLYGON: "Polygon",
      RECTANGLE: "Rectangle",
      CIRCLE: "Circle",
      POINTHEIGHT: "PointHeight",
    };
    // 创建entity集合,方便管理每种entity  二次封装 Cesium.EntityCollection
    this.EntityCollection = {
      POINT: new CesiumEntityCollection(viewer, this.CustomType.POINT),
      POLYLINE: new CesiumEntityCollection(viewer, this.CustomType.POLYLINE),
      POLYGON: new CesiumEntityCollection(viewer, this.CustomType.POLYGON),
      RECTANGLE: new CesiumEntityCollection(viewer, this.CustomType.RECTANGLE),
      CIRCLE: new CesiumEntityCollection(viewer, this.CustomType.CIRCLE),
      POINTHEIGHT: new CesiumEntityCollection(
        viewer,
        this.CustomType.POINTHEIGHT
      ),
    };
    this.initDraw(); // 实例化所有的派生类
  }
  initDraw() {
    // 实例化-派生类-绘制点
    this.drawPoint = new DrawPoint({
      customType: this.CustomType.POINT,
      entityCollection: this.EntityCollection.POINT,
      viewer: this.viewer,
      ellipsoid: this.ellipsoid,
      _tip: this._tip,
      _caculater: this._caculater,
      _coorder: this._coorder,
      _handler: this._handler,
    });
    // 实例化-派生类-绘制折线
    this.drawPolyline = new DrawPolyline({
      customType: this.CustomType.POLYLINE,
      entityCollection: this.EntityCollection.POLYLINE,
      viewer: this.viewer,
      ellipsoid: this.ellipsoid,
      _tip: this._tip,
      _caculater: this._caculater,
      _coorder: this._coorder,
      _handler: this._handler,
    });
    // 实例化-派生类-绘制多边形
    this.drawPolygon = new DrawPolygon({
      customType: this.CustomType.POLYGON,
      entityCollection: this.EntityCollection.POLYGON,
      viewer: this.viewer,
      ellipsoid: this.ellipsoid,
      _tip: this._tip,
      _caculater: this._caculater,
      _coorder: this._coorder,
      _handler: this._handler,
    });
    // 实例化-派生类-绘制多边形
    this.drawRectangle = new DrawRectangle({
      customType: this.CustomType.RECTANGLE,
      entityCollection: this.EntityCollection.RECTANGLE,
      viewer: this.viewer,
      ellipsoid: this.ellipsoid,
      _tip: this._tip,
      _caculater: this._caculater,
      _coorder: this._coorder,
      _handler: this._handler,
    });
    // 实例化-派生类-绘制多边形
    this.drawCircle = new DrawCircle({
      customType: this.CustomType.CIRCLE,
      entityCollection: this.EntityCollection.CIRCLE,
      viewer: this.viewer,
      ellipsoid: this.ellipsoid,
      _tip: this._tip,
      _caculater: this._caculater,
      _coorder: this._coorder,
      _handler: this._handler,
    });
    // 实例化-派生类-绘制点-高程
    this.drawPointHeight = new DrawPointHeight({
      customType: this.CustomType.POINTHEIGHT,
      entityCollection: this.EntityCollection.POINTHEIGHT,
      viewer: this.viewer,
      ellipsoid: this.ellipsoid,
      _tip: this._tip,
      _caculater: this._caculater,
      _coorder: this._coorder,
      _handler: this._handler,
    });
  }
  /**
   * 开始绘制动作
   * @param { this.CustomType 的类型 } type
   * @param { 是否显示测量Label } showMeasureLabel
   */
  startDraw(type, showMeasureLabel = true) {
    const { POINT, POLYLINE, POLYGON, RECTANGLE, CIRCLE, POINTHEIGHT } =
      this.CustomType;
    switch (type) {
      case POINT: {
        this.drawPoint.startDraw(showMeasureLabel);
        break;
      }
      case POLYLINE: {
        this.drawPolyline.startDraw(showMeasureLabel);
        break;
      }
      case POLYGON: {
        this.drawPolygon.startDraw(showMeasureLabel);
        break;
      }
      case RECTANGLE: {
        this.drawRectangle.startDraw(showMeasureLabel);
        break;
      }
      case CIRCLE: {
        this.drawCircle.startDraw(showMeasureLabel);
        break;
      }
      case POINTHEIGHT: {
        this.drawPointHeight.startDraw(showMeasureLabel);
        break;
      }
      default: {
        throw new Error("type 类型错误");
      }
    }
  }
  /**
   * 结束绘制动作
   * @param { this.CustomType 的类型 } type
   */
  endDraw(type) {
    const { POINT, POLYLINE, POLYGON, RECTANGLE, CIRCLE, POINTHEIGHT } =
      this.CustomType;
    switch (type) {
      case POINT: {
        this.drawPoint.endDraw();
        break;
      }
      case POLYLINE: {
        this.drawPolyline.endDraw();
        break;
      }
      case POLYGON: {
        this.drawPolygon.endDraw();
        break;
      }
      case RECTANGLE: {
        this.drawRectangle.endDraw();
        break;
      }
      case CIRCLE: {
        this.drawCircle.endDraw();
        break;
      }
      case POINTHEIGHT: {
        this.drawPointHeight.endDraw();
        break;
      }
      default: {
        throw new Error("type 类型错误");
      }
    }
  }
  /**
   * 移除绘制图形
   * @param {*} type
   */
  removeDraw(type) {
    const { POINT, POLYLINE, POLYGON, RECTANGLE, CIRCLE, POINTHEIGHT } =
      this.CustomType;
    switch (type) {
      case POINT: {
        this.drawPoint.removeDraw();
        break;
      }
      case POLYLINE: {
        this.drawPolyline.removeDraw();
        break;
      }
      case POLYGON: {
        this.drawPolygon.removeDraw();
        break;
      }
      case RECTANGLE: {
        this.drawRectangle.removeDraw();
        break;
      }
      case CIRCLE: {
        this.drawCircle.removeDraw();
        break;
      }
      case POINTHEIGHT: {
        this.drawPointHeight.removeDraw();
        break;
      }
      default: {
        throw new Error("type 类型错误");
      }
    }
  }
  /**
   * 获取实体
   * @param {*} type
   */
  getDraw(type) {
    const { POINT, POLYLINE, POLYGON, RECTANGLE, CIRCLE, POINTHEIGHT } =
      this.CustomType;
    let result = null;
    switch (type) {
      case POINT: {
        result = this.drawPoint.getAllEntity();
        break;
      }
      case POLYLINE: {
        result = this.drawPolyline.getAllEntity();
        break;
      }
      case POLYGON: {
        result = this.drawPolygon.getAllEntity();
        break;
      }
      case RECTANGLE: {
        result = this.drawRectangle.getAllEntity();
        break;
      }
      case CIRCLE: {
        result = this.drawCircle.getAllEntity();
        break;
      }
      case POINTHEIGHT: {
        result = this.drawPointHeight.getAllEntity();
        break;
      }
      default: {
        throw new Error("type 类型错误");
      }
    }
    return result
  }
}
